package com.fuyao.cloud.admin.flow.service.flowable.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fuyao.cloud.admin.flow.dto.InstanceQueryParamsVo;
import com.fuyao.cloud.admin.flow.dto.ProcessInstanceVo;
import com.fuyao.cloud.admin.flow.dto.TaskQueryParamsVo;
import com.fuyao.cloud.admin.flow.dto.TaskVo;
import com.fuyao.cloud.admin.flow.mapper.IFlowableProcessInstanceMapper;
import com.fuyao.cloud.admin.flow.mapper.IFlowableTaskMapper;
import com.fuyao.cloud.admin.flow.model.PagerModel;
import com.fuyao.cloud.admin.flow.model.Query;
import com.fuyao.cloud.admin.flow.service.flowable.IFlowableActHiActinstService;
import com.fuyao.cloud.admin.flow.service.flowable.IFlowableTaskService;
import com.fuyao.cloud.admin.flow.util.DurationUtils;
import com.fuyao.cloud.common.core.flowable.enums.ProcessStatusEnum;
import com.fuyao.cloud.common.security.util.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class FlowableTaskServiceImpl implements IFlowableTaskService {

    @Autowired
    private IFlowableTaskMapper flowableTaskMapper;
    @Autowired
    IFlowableActHiActinstService flowableActHiActinstService;
    @Autowired
    private IFlowableProcessInstanceMapper iFlowableProcessInstanceMapper;

    @Override
    public Long getAppingTaskCont(TaskQueryParamsVo params) {
        params.setUserCode(SecurityUtils.getUser().getUsername());
        return flowableTaskMapper.getAppingTaskCont(params);
    }

    @Override
    public PagerModel<TaskVo> getAppingTasksPagerModel(TaskQueryParamsVo paramsvo, Query query) {
        IPage<TaskVo> queryPage = new Page<>(query.getPageNum(), query.getPageSize());
        paramsvo.setUserCode(SecurityUtils.getUser().getUsername());
        IPage<TaskVo> page = flowableTaskMapper.getAppingTasksPagerModel(queryPage, paramsvo);
        List<TaskVo> taskVoList = page.getRecords();
        this.setTaskVoData(taskVoList);
        return new PagerModel<>(page.getTotal(), page.getRecords());
    }

    @Override
    public PagerModel<TaskVo> getApplyedTasksPagerModel(TaskQueryParamsVo paramsVo, Query query) {
        IPage<TaskVo> queryPage = new Page<>(query.getPageNum(), query.getPageSize());
        paramsVo.setUserCode(SecurityUtils.getUser().getUsername());
        IPage<TaskVo> page = flowableTaskMapper.getApplyedTasksPagerModel(queryPage, paramsVo);
        List<TaskVo> taskVoList = page.getRecords();
        this.setTaskVoData(taskVoList);
        return new PagerModel<>(page.getTotal(), taskVoList);
    }

    private void setTaskVoData(List<TaskVo> taskVoList) {
        if (CollUtil.isNotEmpty(taskVoList)) {
            long currTime = System.currentTimeMillis();
            for (TaskVo taskVo : taskVoList) {
                taskVo.setProcessStatusName(ProcessStatusEnum.getEnumMsgByType(taskVo.getProcessStatus()));
                long time = taskVo.getCreateTime().getTime();
                long duration = currTime - time;
                taskVo.setStayHour(DurationUtils.getDuration(duration));
                Date startTime = taskVo.getStartTime();
                Date finishedTime = taskVo.getFinishedTime();
                if (taskVo.getFinishedTime() == null) {
                    finishedTime = new Date();
                }
                long totalDuration = finishedTime.getTime() - startTime.getTime();
                taskVo.setTotalTime(DurationUtils.getDuration(totalDuration));
            }
        }
    }

    @Override
    public PagerModel<ProcessInstanceVo> getMySendTaskPageModel(InstanceQueryParamsVo paramsVo, Query query) {
        IPage<ProcessInstanceVo> queryPage = new Page<>(query.getPageNum(), query.getPageSize());
        paramsVo.setUserCode(SecurityUtils.getUser().getUsername());
        IPage<ProcessInstanceVo> page = iFlowableProcessInstanceMapper.getMySendTaskPageModel(queryPage, paramsVo);
        return new PagerModel<>(page.getTotal(), page.getRecords());
    }
}
